package com.cdh.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdh.base.DIYException;
import com.cdh.edu.entity.Chapter;
import com.cdh.edu.entity.Video;
import com.cdh.edu.entity.vo.ChapterVo;
import com.cdh.edu.entity.vo.VideoVo;
import com.cdh.edu.feign.VodService;
import com.cdh.edu.mapper.ChapterMapper;
import com.cdh.edu.service.ChapterService;
import com.cdh.edu.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cdh
 * @since 2020-10-20
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;


    //课程大纲列表,根据课程ID进行查询
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        // 1 根据课程id查询课程里面所有的章节
        QueryWrapper<Chapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id",courseId);
        chapterWrapper.orderByDesc("sort","id");
        List<Chapter> chapters = baseMapper.selectList(chapterWrapper);

        // 2 根据课程id查询课程里面所有的小节
        QueryWrapper<Video> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id",courseId);
        videoWrapper.orderByDesc("sort","id");
        List<Video> videoList = videoService.list(videoWrapper);

        // 创建list集合，用于封装最终数据
        List<ChapterVo> finalList = new ArrayList<>();

        // 3 遍历查询章节list集合进行封装
        for (int i = 0; i < chapters.size(); i++) {
            Chapter tChapter = chapters.get(i);
            ChapterVo chapterVo = new ChapterVo();
            // tChapter对象值复制到chapterVo
            BeanUtils.copyProperties(tChapter, chapterVo);
            finalList.add(chapterVo);

            // 创建用于封装章节的小节的集合
            List<VideoVo> videoVoList = new ArrayList<>();
            // 4 遍历查询小节list集合
            for (int m = 0; m < videoList.size(); m++) {
                Video video = videoList.get(m);
                // 判断：小节里面chapterId和章节里面id是否一样
                if (video.getChapterId().equals(tChapter.getId())) {
                    //进行封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    videoVoList.add(videoVo);
                }
            }
            //把封装之后的小节list放到章节对象里面
            chapterVo.setChildren(videoVoList);
        }
        return finalList;
    }

    // 根据章节ID删除章节
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteChapterById(String chapterId) {

        //删除课程视频
        videoService.removeVideoByChapterId(chapterId);

        QueryWrapper<Video> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("chapter_id",chapterId);
        int videoCount = videoService.count(videoWrapper);
        if (videoCount > 0){
            //查询出小节，不进行删除
            throw new DIYException(201,"不能删除");
        }else{
            //没有小节，进行删除
            int res = baseMapper.deleteById(chapterId);
            return res > 0;
        }

    }

}
